//
//  NSObject+KVO.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/5/2.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "NSObject+KVO.h"
#import <objc/runtime.h>


@implementation NSObject (KVO)


static NSString *listionActionKey = @"listionActionKey";
static NSString *keyPathKey = @"keyPathKey";
- (void)setAction:(listionAction)action {
    objc_setAssociatedObject(self, &listionActionKey, action, OBJC_ASSOCIATION_COPY);
}

- (listionAction)action {
    return objc_getAssociatedObject(self, &listionActionKey);
}

- (void)setKeyPath:(NSString *)keyPath {
    objc_setAssociatedObject(self, &keyPathKey, keyPath, OBJC_ASSOCIATION_COPY);
}

- (NSString *)keyPath {
    return objc_getAssociatedObject(self, &keyPathKey);
}

- (void) xxgAddObserver:(NSObject *)object forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)option context:(void *)context andAction:(listionAction)actions {
    [object addObserver:self forKeyPath:keyPath options:option context:context];
    self.action = actions;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    self.action(keyPath, object, change);
}


@end
